package cn.itenzo.controller;

import cn.itenzo.common.vo.RetMsg;
import cn.itenzo.good.model.Good;
import cn.itenzo.good.service.GoodService;
import cn.itenzo.vo.ReqParam;
import cn.itenzo.vo.RetList;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2017/5/30 0030.
 */
@Controller
@RequestMapping(value = "good")
public class GoodController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodController.class);

    @Reference
    private GoodService goodService;


    @ApiOperation(value = "列出商品信息")
    @ResponseBody
    @RequestMapping(value = "l",method = RequestMethod.POST)
    public RetList GoodList(Long id, @RequestBody ReqParam param) {

        LOGGER.info("list good ,storeid:{}",id);
        Good good = new Good();
        int page = param.getPage();
        good.setGoodStoreId(id);
        PageInfo<Good> goodItemPageInfo =  goodService.queryPageByStore(good, page, param.getLimit(), param.getOrderCluse());
        RetList retList = new RetList(goodItemPageInfo.getTotal(),goodItemPageInfo.getList());


        return retList;
    }

    @ApiOperation(value = "添加商品")
    @ResponseBody
    @RequestMapping(value = "a",method = RequestMethod.POST)
    public RetMsg GoodAdd(Good good){
        RetMsg retMsg = goodService.addGood(good);
        return retMsg;
    }


    @ApiOperation(value = "修改商品价格")
    @ResponseBody
    @RequestMapping(value = "modifyPrice",method = RequestMethod.POST)
    public RetMsg modifyGoodPic(String goodId,String money){
        try {
            RetMsg map = goodService.updatePrice(Long.parseLong(goodId), new BigDecimal(money));
            return map;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(),e.getStackTrace());
        }
        return  RetMsg.error("修改价格失败");
    }


    @ApiOperation(value = "修改商品名称")
    @ResponseBody
    @RequestMapping(value = "modifyName",method = RequestMethod.POST)
    public RetMsg modifyGoodName(String goodId,String GoodName){
        try {
            RetMsg map = goodService.updateGoodName(Long.parseLong(goodId),GoodName);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage(),e.getStackTrace());
        }
        return  RetMsg.error("修改名称失败");
    }


    @ApiOperation(value = "商品上架")
    @ResponseBody
    @RequestMapping(value = "shelves/{goodId}",method = RequestMethod.POST)
    public RetMsg pushGood(@PathVariable("goodId") Long goodId){
        try {
            return goodService.pushGood(goodId);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(),e.getStackTrace());
        }
        return null;
    }

    @ApiOperation(value = "商品下架")
    @ResponseBody
    @RequestMapping(value = "down/{goodId}",method = RequestMethod.POST)
    public RetMsg downGood(@PathVariable("goodId") Long goodId){
        try {

            return goodService.pushDownGood(goodId);
        } catch (Exception e) {
            LOGGER.error(e.getLocalizedMessage());
        }
        return null;
    }

    @ApiOperation(value = "商品推荐")
    @ResponseBody
    @RequestMapping(value = "recommend/{goodId}",method = RequestMethod.POST)
    public RetMsg recommend(@PathVariable("goodId") Long goodId){
        try {

            return goodService.recommend(goodId);
        } catch (Exception e) {
            LOGGER.error(e.getLocalizedMessage());
        }
        return null;
    }
    @ApiOperation(value = "商品取消推荐")
    @ResponseBody
    @RequestMapping(value = "unrecommend/{goodId}",method = RequestMethod.POST)
    public RetMsg unrecommend(@PathVariable("goodId") Long goodId){
        try {

            return goodService.unReCommend(goodId);
        } catch (Exception e) {
            LOGGER.error(e.getLocalizedMessage());
        }
        return null;
    }

}
